﻿#include "ScrollBar.h"
#include <QPainter>
#include <QtSvg/QSvgRenderer>
#include <QDir>
#include <QEvent>
#include <QMouseEvent>
#include <QDebug>

ScrollBar::ScrollBar(QWidget *parent) : QWidget(parent)
{
    this->setMinimumWidth(20);
    this->setMouseTracking(true);
}

void ScrollBar::setCurrentIndex(const int &index)
{
    m_iCurrent = index;
}

void ScrollBar::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);

    float fDeltH = (this->height() - BACKGROUND_WIDTH) / 26.0;
    if(m_indexRectList.isEmpty())
    {
        m_backgroundRect = QRectF(this->width() / 2 - BACKGROUND_WIDTH / 2.0, 0, BACKGROUND_WIDTH, this->height() - 1);

        int i = 0;
        while(i <= 25)
        {
            QRectF textRect(0, fDeltH * i + BACKGROUND_WIDTH / 2.0,  this->width(), fDeltH);
            m_indexRectList.append(textRect);
            i++;
        }
    }

    // 绘制背景
    drawBackground(&painter);
    // 绘制字母
    drawIndex(&painter);

    // 绘制当前选中
    drawCurrendIndex(&painter, fDeltH);
}

void ScrollBar::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);
    if(event->button() == Qt::LeftButton)
    {
        for(int i = 0; i < m_indexRectList.size(); i++)
        {
            if(m_indexRectList.at(i).contains(event->pos()))
            {
                m_iCurrent = i;
                m_bMousePressed = true;
                m_posPressed = m_indexRectList.at(i).center().toPoint();
                Q_EMIT sigCurrentIndexChanged(m_iCurrent);
            }
        }
    }
    this->update();
}

void ScrollBar::mouseMoveEvent(QMouseEvent *event)
{
    QWidget::mouseMoveEvent(event);
    if(m_bMousePressed)
    {
        m_bDrawCurrentIndex = true;
        int iDelt = event->pos().y() - m_posPressed.y();
        float fDeltH = (this->height() - BACKGROUND_WIDTH) / 26.0;
        int iSpan = iDelt / fDeltH;
        if(qAbs(iSpan) > 0)
        {
            m_iCurrent += iSpan;
            if(m_iCurrent < 0)
            {
                m_iCurrent = 0;
            }

            if(m_iCurrent > 25)
            {
                m_iCurrent = 25;
            }

            Q_EMIT sigCurrentIndexChanged(m_iCurrent);

            m_posPressed = QRectF(0, fDeltH * m_iCurrent + BACKGROUND_WIDTH / 2.0,  this->width(), fDeltH).center().toPoint();
        }
    }
    else
    {
        QRectF rect(0,0, BACKGROUND_WIDTH, this->height());
        rect.moveCenter(QPointF(this->width() / 2.0, this->height() / 2.0));
        if(rect.contains(event->pos()))
        {
            m_bDrawCurrentIndex = true;
        }
        else
        {
            m_bDrawCurrentIndex = false;
        }
    }
    this->update();
}

void ScrollBar::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);
    if(event->button() == Qt::LeftButton)
    {
        m_bMousePressed = false;
    }
}

void ScrollBar::drawBackground(QPainter *painter)
{
    // 绘制背景
    painter->save();
    painter->setPen(Qt::NoPen);
    QColor backColor;
    backColor.setNamedColor("#F3F3F3");
    painter->setBrush(backColor);
    painter->drawRoundedRect(m_backgroundRect, 5, 5);
    painter->restore();
}

void ScrollBar::drawIndex(QPainter *painter)
{
    painter->save();
    QFont font = painter->font();
//    font.setFamily("Microsoft YaHei");
//    font.setPixelSize(9);
    painter->setFont(font);

    int i = 0;
    while(i <= 25)
    {
        painter->drawText(m_indexRectList.at(i), Qt::AlignHCenter | Qt::AlignVCenter, QString('A' + i));
        i++;
    }
    painter->restore();
}

void ScrollBar::drawCurrendIndex(QPainter *painter, const float fDeltH)
{
    if(m_bDrawCurrentIndex == false)
    {
        return;
    }

    // 绘制当前选中
    painter->save();
    painter->setBrush(Qt::white);
    painter->setPen(Qt::NoPen);
    QRectF curRect = m_indexRectList.at(m_iCurrent);
    QPointF selRectCenter = curRect.center();
    QRectF selRect(0, 0, 18, 18);
    selRect.moveCenter(selRectCenter);
    painter->drawEllipse(selRect);
    painter->restore();

    painter->save();
    QColor selColor;
    selColor.setNamedColor("#00B0AE");
    painter->setPen(selColor);
    QFont font = painter->font();
    font.setPixelSize(13);
    painter->setFont(font);
    QSvgRenderer render;
    render.load(QDir::currentPath() + "/IndexSelect.svg");
    render.render(painter, selRect);
    painter->drawText(selRect, Qt::AlignHCenter | Qt::AlignVCenter, QString('A' + m_iCurrent));
    painter->restore();
}
